package com.lifang.imsoa.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.lifang.json.JsonTool;

/**
 * 针对List<List<T>>和Map<String,List<T>>这种特殊的对象和json相互转化
 * 
 * @author Andy
 * 
 */
public class JsonUtil {

	@SuppressWarnings("deprecation")
	public static <T> String writeListList2String(List<List<T>> list) {
		List<String> jsonList = new ArrayList<>();
		for (List<T> l : list) {
			jsonList.add(JsonTool.writeValueAsString(l));
		}
		return JsonTool.writeValueAsString(jsonList);
	}

	@SuppressWarnings("deprecation")
	public static <T> List<List<T>> readString2ListList(String json, Class<T> tClass) {
		List<List<T>> listList = new ArrayList<>();
		List<String> jsonList = JsonTool.readValue2List(json, String.class);
		for (String str : jsonList) {
			List<T> l = JsonTool.readValue2List(str, tClass);
			listList.add(l);
		}
		return listList;
	}

	@SuppressWarnings("deprecation")
	public static <T> String writeMap2String(Map<String, List<T>> map) {
		Map<String, String> newMap = new HashMap<String, String>();
		for (Entry<String, List<T>> entry : map.entrySet()) {
			newMap.put(entry.getKey(), JsonTool.writeValueAsString(entry.getValue()));
		}
		return JsonTool.writeValueAsString(newMap);
	}

	@SuppressWarnings("deprecation")
	public static <T> Map<String, List<T>> readString2Map(String json, Class<T> tClass) {
		Map<String, List<T>> newMap = new HashMap<String, List<T>>();
		Map<String, String> map = JsonTool.readValue2Map(json, String.class, String.class);
		for (Entry<String, String> entry : map.entrySet()) {
			newMap.put(entry.getKey(), JsonTool.readValue2List(entry.getValue(), tClass));
		}
		return newMap;
	}
}
